package com.wechat.service.quartz.config;

import java.util.Properties;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 使用spring管理Scheduler
 * @author admin
 *
 */
@Configuration
@EnableConfigurationProperties(value = QuartzProperties.class)
public class QuartzConfig {
	private final static Logger log = LoggerFactory.getLogger(QuartzConfig.class);
	/**
	 * 线程池 线程数
	 */
	@Autowired
	private QuartzProperties quartzProperties;

	@Bean(name = "scheduler")
	public Scheduler getScheduler() {
		try {
			StdSchedulerFactory sf = new StdSchedulerFactory();
			Properties props = new Properties();
			props.put("org.quartz.threadPool.threadCount", quartzProperties.getThreadCount());// 线程数
			sf.initialize(props);
			return sf.getScheduler();
		} catch (SchedulerException e) {
			log.error("Scheduler 初始化失败!");
			e.printStackTrace();
		}
		return null;
	}

}
